package com.zzh.wsss.controller;

        import com.zzh.wsss.service.AdminInterfaceService;
        import com.zzh.wsss.service.UserLoginService;
        import org.apache.poi.ss.usermodel.CellType;
        import org.apache.poi.xssf.usermodel.XSSFCell;
        import org.apache.poi.xssf.usermodel.XSSFRow;
        import org.apache.poi.xssf.usermodel.XSSFSheet;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;
        import org.springframework.web.multipart.MultipartFile;

        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

@RestController
@CrossOrigin
public class AdminInterfaceController {

    @Autowired
    private AdminInterfaceService adminInterfaceService;

    @RequestMapping(value = "/getUsersMsg",method = RequestMethod.GET)
    private Map<String,Object> getUsersMsg(String currentPage, String search){
        return adminInterfaceService.getUsersMsg(currentPage,search);
    }

    @RequestMapping(value = "/getJobsMsg",method = RequestMethod.GET)
    private Map<String,Object> getJobsMsg(String currentPage, String search){
        return adminInterfaceService.getJobsMsg(currentPage,search);
    }

    @RequestMapping(value = "/getApplysMsg",method = RequestMethod.GET)
    private Map<String,Object> getApplysMsg(String currentPage){
        return adminInterfaceService.getApplysMsg(currentPage);
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    private Map<String,Object> addUser(@RequestBody Map<String,String> map){
        return adminInterfaceService.addUser(map.get("username"),map.get("password"));
    }

    @RequestMapping(value = "/addJob",method = RequestMethod.POST)
    private Map<String,Object> addJob(@RequestBody Map<String,String> map){
        return adminInterfaceService.addJob(map.get("jobName"),map.get("number"),map.get("introduce"));
    }

    @RequestMapping(value = "/addUsers",method = RequestMethod.POST)
    public Map<String,Object>  addUsers(MultipartFile file) {
        Map<String, Object> map= new HashMap<String,Object>();
        List<Integer> addResult=new ArrayList<>();
        try {
            XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());//由输入流文件得到工作簿对象
            XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
            int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
            for (int i = 1; i <= lastRowNum; i++) {
                XSSFRow row = sheet.getRow(i);//获取每一行
                XSSFCell cell = row.getCell(0);
                XSSFCell cell1 = row.getCell(1);
                cell.setCellType(CellType.STRING);
                cell1.setCellType(CellType.STRING);
                String username = cell.getStringCellValue();
                String password = cell1.getStringCellValue();
                if(!(Boolean) adminInterfaceService.addUser(username,password).get("result")){
                    addResult.add(i);
                }
            }
            wb.close();
            map.put("result",true);
        } catch (Exception e) {
            map.put("result",false);
        }

        map.put("addFails",addResult);
        return map;
    }

    @RequestMapping(value = "/addJobs",method = RequestMethod.POST)
    public Map<String,Object>  addJobs(MultipartFile file) {
        Map<String, Object> map= new HashMap<String,Object>();
        List<Integer> addResult=new ArrayList<>();
        try {
            XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());//由输入流文件得到工作簿对象
            XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
            int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
            for (int i = 1; i <= lastRowNum; i++) {
                XSSFRow row = sheet.getRow(i);//获取每一行
                XSSFCell cell = row.getCell(0);
                XSSFCell cell1 = row.getCell(1);
                XSSFCell cell2 = row.getCell(2);
                cell.setCellType(CellType.STRING);
                cell1.setCellType(CellType.STRING);


                String introduce;
                if(cell2!=null){
                    cell2.setCellType(CellType.STRING);
                    introduce = cell2.getStringCellValue();
                }else{
                    introduce = "";
                }
                String jobName = cell.getStringCellValue();
                String number = cell1.getStringCellValue();

                if(!(Boolean) adminInterfaceService.addJob(jobName,number,introduce).get("result")){
                    addResult.add(i);
                }
            }
            wb.close();
            map.put("result",true);
        } catch (Exception e) {
            map.put("result",false);
        }

        map.put("addFails",addResult);
        return map;
    }

    @RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE)
    private Map<String,Object> deleteUser(@RequestBody Map<String,String> map){
        return adminInterfaceService.deleteUser(map.get("userId"));
    }

    @RequestMapping(value = "/deleteJob",method = RequestMethod.DELETE)
    private Map<String,Object> deleteJob(@RequestBody Map<String,String> map){
        return adminInterfaceService.deleteJob(map.get("jobId"));
    }

    @RequestMapping(value = "/updataUserMsg",method = RequestMethod.PUT)
    private Map<String,Object> updataUserMsg(@RequestBody Map<String,String> map){
        return adminInterfaceService.updataUserMsg(map.get("username"),map.get("password"),map.get("jobId"));
    }

    @RequestMapping(value = "/updataJobMsg",method = RequestMethod.PUT)
    private Map<String,Object> updataJobMsg(@RequestBody Map<String,String> map){
        return adminInterfaceService.updataJobMsg(map.get("jobId"),map.get("jobName"),map.get("number"),map.get("introduce"));
    }

    @RequestMapping(value = "/applyProcess",method = RequestMethod.PUT)
    private Map<String,Object> applyProcess(@RequestBody Map<String,String> map){
        return adminInterfaceService.applyProcess(map.get("applyId"),map.get("results"));
    }
}
